package com.example.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.Base.BasePersonActivity;
import com.example.adapter.DividerItemDecoration;
import com.example.adapter.MyItemClickListener;
import com.example.administrator.olddoctor.R;
import com.example.tools.StringUtils;
import com.example.tools.httputils.ok.HttpUtils;
import com.example.view.CircleImageView;
import com.xdandroid.simplerecyclerview.Adapter;
import com.xdandroid.simplerecyclerview.SimpleSwipeRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.RequestBody;


/**
 * 我的消息
 */
public class MessageActivity extends BasePersonActivity implements View.OnClickListener,MyItemClickListener,SwipeRefreshLayout.OnRefreshListener {

    private Button is_attention4;
    private Button is_attention;
    private RecyclerView recyclerView;
    private SimpleSwipeRefreshLayout message_SwipeRefresh;
    private MyAdapter myAdapter;
    private ArrayList<Map<String,Object>> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_message);
        init();
        initData();
    }

    private void init() {
        setActionBar(getResources().getDrawable(R.mipmap.call_back), null,"我的消息",null,null,null);
        recyclerView = (RecyclerView) findViewById(R.id.message_recyclerView);
        message_SwipeRefresh = (SimpleSwipeRefreshLayout) findViewById(R.id.message_SwipeRefresh);
        //设置布局管理器
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //设置Item增加、移除动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.VERTICAL ));
        message_SwipeRefresh.setColorSchemeResources(R.color.colorAccent);
        message_SwipeRefresh.setOnRefreshListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.actionbar_left:
                finish();
                break;
        }
    }

    private void initData() {
        list = new ArrayList();
        Map<String,Object>map = new HashMap<>();
        map.put("username","名字a");
        map.put("time","刚刚a");
        map.put("nomal","刚刚关注了你a");
        map.put("isattention",true);
        map.put("type",1);
        list.add(map);
        Map<String,Object>map7 = new HashMap<>();
        map7.put("username","名字b");
        map7.put("time","刚刚b");
        map7.put("nomal","关注了你b");
        map7.put("isattention",false);
        map7.put("type",1);
        list.add(map7);
        Map<String,Object>map8 = new HashMap<>();
        map8.put("username","名字b");
        map8.put("time","刚刚b");
        map8.put("nomal","关注了你b");
        map8.put("isattention",false);
        map8.put("type",1);
        list.add(map8);
        Map<String,Object>map9 = new HashMap<>();
        map9.put("username","名字b");
        map9.put("time","刚刚b");
        map9.put("nomal","关注了你b");
        map9.put("isattention",false);
        map9.put("type",1);
        list.add(map9);
        Map<String,Object>map10 = new HashMap<>();
        map10.put("username","名字b");
        map10.put("time","刚刚b");
        map10.put("nomal","关注了你b");
        map10.put("isattention",false);
        map10.put("type",1);
        list.add(map10);
        Map<String,Object>map2 = new HashMap<>();
        map2.put("username","名字b");
        map2.put("time","刚刚b");
        map2.put("nomal","关注了你b");
        map2.put("isattention",false);
        map2.put("type",1);
        list.add(map2);
        Map<String,Object>map3 = new HashMap<>();
        map3.put("username","名字c");
        map3.put("time","刚刚c");
        map3.put("nomal","关注了你");
        map3.put("isattention",false);
        map3.put("type",1);
        list.add(map3);
        Map<String,Object>map4 = new HashMap<>();
        map4.put("username","名字d");
        map4.put("time","刚刚d");
        map4.put("nomal","评论了你的主题d");
        map4.put("content","评论了d");
        map4.put("type",2);
        list.add(map4);
        Map<String,Object>map5 = new HashMap<>();
        map5.put("username","名字e");
        map5.put("time","刚刚e");
        map5.put("nomal","你关注的话题更新了99条视频，快去看看吧e");
        map5.put("type",2);
        list.add(map5);
        Map<String,Object>map6 = new HashMap<>();
        map6.put("username","名字f");
        map6.put("time","刚刚f");
        map6.put("nomal","赞了你的视频f");
        map6.put("bitmap",BitmapFactory.decodeResource(getResources(),R.mipmap.image));
        map6.put("type",3);
        list.add(map6);
        //设置adapter
        myAdapter = new MyAdapter(this){
            protected void onLoadMore(Void v) {
                Log.e("banhenan", "onLoadMore");
            }
        };
        myAdapter.load(list);
        recyclerView.setAdapter(myAdapter);

    }


    @Override
    public void itemClickListener(View view, int position) {
        showToast(list.get(position).get("username")+"=="+position);
        Log.e("===="+list.get(position).get("username"),"===="+position);
    }

    @Override
    public void onRefresh() {
        showToast("成功");
    }



    public void swipeRefreshColor(){
        message_SwipeRefresh.setRefreshing(false);
    }

     class MyAdapter extends RecyclerView.Adapter {
        private MyItemClickListener listener;
        private ArrayList<Map<String,Object>> list;
        public MyAdapter(MyItemClickListener listener) {this.listener = listener;}

        public void load(ArrayList<Map<String,Object>> list){
            if(null == this.list){
                this.list = list;
            } else {
                this.list.addAll(list);
            }
        }

        public void clearDate(){
            this.list.clear();
            this.notifyDataSetChanged();
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            switch (viewType){
                case 1:return new myAttentionHolder(LayoutInflater.from(MessageActivity.this).inflate(R.layout.my_messate_attention, parent,false),listener);
                case 2:return new mynomalHolder(LayoutInflater.from(MessageActivity.this).inflate(R.layout.my_message_nomal_item, parent,false),listener);
                case 3:return new myimageHolder(LayoutInflater.from(MessageActivity.this).inflate(R.layout.my_message_image,parent,false),listener);
            }
                    return null;
        }

        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

            if(holder instanceof myAttentionHolder){
                ((myAttentionHolder) holder).user_name.setText((String)list.get(position).get("username"));
                ((myAttentionHolder) holder).message_time.setText((String)list.get(position).get("time"));
                ((myAttentionHolder) holder).normal_text.setText((String)list.get(position).get("nomal"));
                ((myAttentionHolder) holder).is_attention.setEnabled((Boolean)list.get(position).get("isattention"));
                ((myAttentionHolder) holder).is_attention.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((myAttentionHolder)holder).is_attention.setText("已关注");
                        ((myAttentionHolder)holder).is_attention.setEnabled(false);
                    }
                });
            } else if(holder instanceof mynomalHolder){
                ((mynomalHolder) holder).user_name.setText((String)list.get(position).get("username"));
                ((mynomalHolder) holder).message_time.setText((String)list.get(position).get("time"));
                ((mynomalHolder) holder).normal_text.setText((String)list.get(position).get("nomal"));
                String content = (String)list.get(position).get("content");
                if(!StringUtils.isEmpty(content)){((mynomalHolder) holder).content_text.setText(content);}
                else { ((mynomalHolder) holder).content_text.setVisibility(View.GONE); }
            } else if(holder instanceof myimageHolder){
                ((myimageHolder) holder).user_name.setText((String)list.get(position).get("username"));
                ((myimageHolder) holder).message_time.setText((String)list.get(position).get("time"));
                ((myimageHolder) holder).normal_text.setText((String)list.get(position).get("nomal"));
                ((myimageHolder) holder).image_right_vidio.setImageBitmap((Bitmap) list.get(position).get("bitmap"));
            }
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        @Override
        public int getItemViewType(int position) {
            Log.e(getClass().getName(),"===="+list.get(position).get("type")+"=="+position);
            if(!list.get(position).containsKey("type")){
                Log.e(getClass().getName(),"必须要有type属性在您的RecycleView的数据源中");
                return 0;
            } else {
                int type = (int) list.get(position).get("type");
                return type;
            }
        }

    }

    class myAttentionHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public CircleImageView user_image;
        public TextView user_name;
        public TextView message_time;
        public TextView normal_text;
        public Button is_attention;
        public MyItemClickListener listener ;

        public myAttentionHolder(View itemView , MyItemClickListener listener) {
            super(itemView);
            user_image = (CircleImageView) itemView.findViewById(R.id.user_image);
            user_name = (TextView) itemView.findViewById(R.id.user_name);
            message_time = (TextView) itemView.findViewById(R.id.message_time);
            normal_text = (TextView) itemView.findViewById(R.id.normal_text);
            is_attention = (Button) itemView.findViewById(R.id.is_attention);
            this.listener = listener;
            itemView.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            listener.itemClickListener(v,getPosition());
        }
    }

    class mynomalHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public CircleImageView user_image;
        public TextView user_name;
        public TextView message_time;
        public TextView normal_text;
        public TextView content_text;
        public MyItemClickListener listener ;

        public mynomalHolder(View itemView, MyItemClickListener listener) {
            super(itemView);
            user_image = (CircleImageView) itemView.findViewById(R.id.user_image);
            user_name = (TextView) itemView.findViewById(R.id.user_name);
            message_time = (TextView) itemView.findViewById(R.id.message_time);
            normal_text = (TextView) itemView.findViewById(R.id.normal_text);
            content_text = (TextView) itemView.findViewById(R.id.content_text);
            this.listener = listener;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            listener.itemClickListener(v,getPosition());
        }
    }

    class myimageHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public CircleImageView user_image;
        public TextView user_name;
        public TextView message_time;
        public TextView normal_text;
        public ImageView image_right_vidio;
        public MyItemClickListener listener ;

        public myimageHolder(View itemView, MyItemClickListener listener) {
            super(itemView);
            user_image = (CircleImageView) itemView.findViewById(R.id.user_image);
            user_name = (TextView) itemView.findViewById(R.id.user_name);
            message_time = (TextView) itemView.findViewById(R.id.message_time);
            normal_text = (TextView) itemView.findViewById(R.id.normal_text);
            image_right_vidio = (ImageView) itemView.findViewById(R.id.image_right_vidio);
            this.listener = listener;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            listener.itemClickListener(v,getPosition());
        }
    }
}
